package com.pai4j.pgc.service.community;
import com.pai4j.common.enums.CommonStatusEnum;
import com.pai4j.domain.community.vo.request.CCNavPathControlRequestVO;
import com.pai4j.domain.community.vo.request.CCNavPathRequestVO;
import com.pai4j.pgc.dao.community.ICCNavPathDAO;
import com.pai4j.pgc.entity.community.CCNavPathEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *
 *
 * @author: CYM-pai
 * @date: 2025/10/27 14:29
 **/
@Slf4j
@Service
public class CCNavPathService {

    @Autowired
    private ICCNavPathDAO pathDAO;
    @Autowired
    private CCNavCoreCacheService coreCacheService;

    /***
     * 保存更新栏目
     * @param vo
     * @param operator
     * @return
     */
    public Long save(CCNavPathRequestVO vo, String operator) {
        CCNavPathEntity pathEntity = this.convertPathEntity(vo, operator);
        Long id = pathDAO.save(pathEntity).getId();
        // 刷新客户端导航缓存
        coreCacheService.refreshClientNavCache(pathEntity.getCcId());
        return id;
    }

    /***
     * 更新栏目状态
     * @param vo
     * @param operator
     */
    public void updateStatus(CCNavPathControlRequestVO vo, String operator) {
        CCNavPathEntity pathEntity = pathDAO.findAllById(vo.getId());
        Assert.isTrue(pathEntity != null, "修改失败: 查找到栏目");
        pathEntity.setStatus(vo.getStatus());
        pathEntity.setUpdateBy(operator);
        pathDAO.save(pathEntity);
        // 刷新客户端导航缓存
        coreCacheService.refreshClientNavCache(pathEntity.getCcId());
        log.info("{} 修改了栏目 {} 状态为 {}", operator, pathEntity.getName(), vo.getStatus());
    }

    /***
     * 更新栏目标记
     * @param vo
     * @param operator
     */
    public void updateTag(CCNavPathControlRequestVO vo, String operator) {
        CCNavPathEntity pathEntity = pathDAO.findAllById(vo.getId());
        Assert.isTrue(pathEntity != null, "修改失败: 查找到栏目");
        pathEntity.setTag(vo.getTag());
        pathEntity.setUpdateBy(operator);
        pathDAO.save(pathEntity);
        // 刷新客户端导航缓存
        coreCacheService.refreshClientNavCache(pathEntity.getCcId());
        log.info("{} 修改了栏目 {} 标记为 {}", operator, pathEntity.getName(), vo.getTag());
    }

    /***
     * 栏目列表
     * @return
     */
    public List<CCNavPathRequestVO> list(Long ccId, List<String> statusList) {
        List<CCNavPathEntity> pathEntityList;
        if (ccId == null) {
            pathEntityList = pathDAO.findAllByStatusInOrderByTagDescScoreDescIdAsc(statusList);
        } else {
            pathEntityList = pathDAO.findAllByCcIdAndStatusInOrderByTagDescScoreDescIdAsc(ccId, statusList);
        }
        if (CollectionUtils.isEmpty(pathEntityList)) {
            return Collections.emptyList();
        }
        return pathEntityList.stream().map(this::convertPathRequestVO).collect(Collectors.toList());
    }

    /***
     * 查询所有的栏目
     * @return
     */
    public Map<Long, CCNavPathEntity> getAllPathMap() {
        List<CCNavPathEntity> pathEntityList = pathDAO.findAll();
        if (CollectionUtils.isEmpty(pathEntityList)) {
            return Collections.emptyMap();
        }
        return pathEntityList.stream().collect(Collectors.toMap(CCNavPathEntity::getId, path -> path));
    }

    private CCNavPathRequestVO convertPathRequestVO(CCNavPathEntity pathEntity) {
        CCNavPathRequestVO pathRequestVO = new CCNavPathRequestVO();
        pathRequestVO.setId(pathEntity.getId());
        pathRequestVO.setName(pathEntity.getName());
        pathRequestVO.setIcon(pathEntity.getIcon());
        pathRequestVO.setTag(pathEntity.getTag());
        pathRequestVO.setScore(pathEntity.getScore());
        pathRequestVO.setStatus(pathEntity.getStatus());
        pathRequestVO.setMenuType(pathEntity.getMenuType());
        pathRequestVO.setShortName(pathEntity.getShortName());
        return pathRequestVO;
    }

    private CCNavPathEntity convertPathEntity(CCNavPathRequestVO vo, String operator) {
        CCNavPathEntity pathEntity;
        Date now = new Date();
        if (vo.getId() != null) {
            pathEntity = pathDAO.findAllById(vo.getId());
            Assert.isTrue(pathEntity != null, "修改失败:栏目不存在");
        } else {
            pathEntity = new CCNavPathEntity();
            pathEntity.setCcId(vo.getCcId());
            pathEntity.setCreateBy(operator);
            pathEntity.setCreateDate(now);
            pathEntity.setStatus(CommonStatusEnum.NEW.getStatus());
        }
        pathEntity.setIcon(vo.getIcon());
        pathEntity.setMenuType(vo.getMenuType());
        pathEntity.setName(vo.getName());
        pathEntity.setShortName(vo.getShortName() == null ? "" : vo.getShortName());
        pathEntity.setScore(vo.getScore());
        pathEntity.setTag(vo.getTag());
        pathEntity.setUpdateBy(operator);
        pathEntity.setUpdateDate(now);
        return pathEntity;
    }
}
